package aor2.cpu.blockinterrupt;

import aor2.components.Register;
import aor2.components.Restorable;

/**
 * IMR registar maske
 * 
 * @author Erna
 * 
 */
public class Imr extends Register implements Restorable {

	private static final long IMRstate = 0x0e;

	public Imr() {
		super(IMRstate, null, null);
	}

	public enum ImrBitMask {
		IMR1(0x02), IMR2(0x04), IMR3(0x08);

		private ImrBitMask(long mask) {
			this.mask = mask;
		}

		public long getMask() {
			return mask;
		}

		private long mask;
	}

	public boolean imr1() {
		return (state & ImrBitMask.IMR1.getMask()) != 0;
	}

	public boolean imr2() {
		return (state & ImrBitMask.IMR2.getMask()) != 0;
	}

	public boolean imr3() {
		return (state & ImrBitMask.IMR3.getMask()) != 0;
	}

	public void setImr1(boolean active) {
		if (active)
			state |= ImrBitMask.IMR1.getMask();
		else
			state &= ~ImrBitMask.IMR1.getMask();
	}

	public void setImr2(boolean active) {
		if (active)
			state |= ImrBitMask.IMR2.getMask();
		else
			state &= ~ImrBitMask.IMR2.getMask();
	}

	public void setImr3(boolean active) {
		if (active)
			state |= ImrBitMask.IMR3.getMask();
		else
			state &= ~ImrBitMask.IMR3.getMask();
	}

	public void setState(long st) {
		state = st;
	}

	public long getState() {
		return state;
	}

	@Override
	public String toString() {
		return "IMR: " + state + " [imr1:" + imr1() + ", imr2:" + imr2()
				+ ", imr3:" + imr3() + "]";
	}

	@Override
	public void restore(String restorePoint) {
		String trim = restorePoint.replace(beginTag(), "");
		trim = trim.replace(endTag(), "");
		trim = trim.trim();
		setState(Long.parseLong(trim));
	}

	@Override
	public String createRestorePoint() {
		return beginTag() + "\n\t" + state + "\n" + endTag();
	}

	@Override
	public String beginTag() {
		return "<IMR>";
	}

	@Override
	public String endTag() {
		return "</IMR>";
	}

}
